<template>
  <div>
    <h1>登录</h1>

    <input type="text" v-model="username">
    <input type="text" v-model="password">
    <button @click="submit">登录</button>
  </div>
</template>

<script>
  import {setToken} from "@/utils/storage";

  export default {
    data() {
      return {
        username: '',
        password: ''
      }
    },
    name: "Login",
    methods: {
      submit() {
        this.$store.dispatch('users/login', {username: this.username, password: this.password})
          .then(res=>{
            // console.log(res);
            if (res.code === 1) {
              setToken(res.token);
              this.$router.push('/main');
            } else {
              this.$message.error('用户或密码出错')
            }

            // 成功了  code 1  跳转页面
            // 失败 -1 提示
          })
      }
    },

    created() {

    }
  }
</script>

<style scoped>

</style>
